//
//  RankDetailViewController.m
//  Cartoon
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "RankDetailViewController.h"
#import "Rank.h"
#import "RankDetailTableViewCell.h"
#import "CartoonDetailsViewController.h"
@interface RankDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation RankDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight-113) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = NO;
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[RankDetailTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self handleHeaderRefresh];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Rank *model = [self.arr objectAtIndex:indexPath.row];
    RankDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.rankModel = model;
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
    
}

- (void)handleData
{
    [NetHandler dataWithUrl:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Rank/rankDetailForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4/rank_id/%@",self.rank_id] complant:^(id data) {
        self.arr = [NSMutableArray array];
        NSArray *dataArr = [data objectForKey:@"data"];
        self.arr = [Rank modelsWithArr:dataArr];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    }];
}


- (void)handleHeaderRefresh
{
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self handleData];
    }];
    
    // 马上进入刷新状态
    [self.tableView.header beginRefreshing];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CartoonDetailsViewController *carDetail = [[CartoonDetailsViewController alloc] init];
    
    Rank *rank = [self.arr objectAtIndex:indexPath.row];
    
    carDetail.idtext = rank.comic_id;
    
    [self.navigationController pushViewController:carDetail animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
